package utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import data.Carrier;
import data.CarrierType;
import data.Package;

public class IO {

    List<CarrierType> carrierTypes;
    List<Package> packages;
    private Statistics stats;

    public IO(Statistics stats) {
	this.carrierTypes = new ArrayList<CarrierType>();
	this.packages = new ArrayList<Package>();
	this.stats = stats;
    }

    public void readFile(String path) {
	try {
	    this.carrierTypes = new ArrayList<CarrierType>();
	    this.packages = new ArrayList<Package>();
	    List<String> lines = Files.readAllLines(Paths.get(path),
		    StandardCharsets.US_ASCII);
	    int carriersNumber = Integer.parseInt(lines.get(0));
	    for (int i = 0; i < carriersNumber; i++) {
		String[] splittedLine = lines.get(i + 1).split("\t");
		carrierTypes.add(new CarrierType(splittedLine[0],
			splittedLine[1], splittedLine[2], splittedLine[3],
			splittedLine[4]));
	    }
	    for (int i = carriersNumber + 2; i < lines.size(); i++) {
		String[] splittedLine = lines.get(i).split("\t");
		packages.add(new Package(splittedLine[0], splittedLine[1],
			splittedLine[2], splittedLine[3]));
	    }
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    public List<CarrierType> getCarriersFromFile() {
	return carrierTypes;
    }

    public List<Package> getPackagesFromFile() {
	return packages;
    }

    public void writeOutput(List<Carrier> carriers, String name) {
	BufferedWriter bw = null;
	try {
	    File file = new File(name + "_output.txt");
	    if (!file.exists()) {
		file.createNewFile();
	    }
	    FileWriter fw = new FileWriter(file);
	    bw = new BufferedWriter(fw);

	    float fieldBaseSum = 0;
	    double minCapacityObject = Float.MAX_VALUE;

	    StringBuilder secondPart = new StringBuilder();
	    StringBuilder thirdPart = new StringBuilder();

	    int thirdPartCounter = 0;

	    for (Carrier carrier : carriers) {
		// PRZETWARZANIE 1 CZESCI WYJSCIA
		fieldBaseSum += carrier.getType().getFieldBase();
		if (carrier.getCapacity() < minCapacityObject) {
		    minCapacityObject = carrier.getCapacityRoundToModules();
		}
		// PRZETWARZANIE 2 CZESCI WYJSCIA
		double modules = Math.ceil(carrier.getHeight()
			/ carrier.getType().getModuleHeight());
		secondPart.append(carrier.getName() + "\t"
			+ carrier.getType().getName() + "\t" + (int) modules
			+ "\n");
		// PRZETWARZANIE 3 CZESCI WYJSCIA
		for (Package pack : carrier.getPackages()) {
		    thirdPart.append(pack.getId() + "\t" + carrier.getName()
			    + "\n");
		    thirdPartCounter++;
		}
	    }

	    bw.write("1\n");
	    bw.write(fieldBaseSum + "\t" + minCapacityObject + "\n");
	    bw.write(carriers.size() + "\n");
	    bw.write(secondPart.toString());
	    bw.write(thirdPartCounter + "\n");
	    bw.write(thirdPart.toString());

	    bw.close();

	    stats.addFieldBaseSum(name, fieldBaseSum);
	    stats.addMinCapacityObject(name, minCapacityObject);
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
}
